// packageLogin/pages/login/login.js
const app = getApp();
const md5 = require('../../../utils/md5')
let phoneReg = /^[1][0-9]{10}$/
import {
  throttle
} from "../../../utils/util.js"
import {
  signIn
} from "../../../public/api/login.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    translations: {},
    phoneNumber: '',
    password: '',
    jsCode: '',
    loginMsg: {
      phone: '',
      password: '',
    },
    checked: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  doLogin: throttle(function () {
    let that = this
    if (this.data.phoneNumber === '') {
      this.setData({
        ["loginMsg.phone"]: '手机号不可为空'
      })
    }
    if (this.data.password === '') {
      this.setData({
        ["loginMsg.password"]: '密码不可为空'
      })
    }
    if (this.data.loginMsg.phone !== '' || this.data.loginMsg.password !== '') {
      return false
    }
    wx.login({
      success: res => {
        that.doSubmit(res.code)
      },
      fail(err) {
        console.log(err);
      }
    })
  }),

  doSubmit(jsCode) {
    let that = this
    let theData = {
      phoneNumber: this.data.phoneNumber,
      password: md5.hexMD5(this.data.password),
      jsCode
    }
    signIn(theData).then(res => {
      if (res.data.code === 1) {
        app.globalData.userInfo = res.data.data
        wx.setStorageSync('userInfo', res.data.data)
        if(that.data.checked == 1) {
          wx.setStorageSync('account', {phoneNumber: that.data.phoneNumber,password: that.data.password})
        } else {
          wx.removeStorageSync('account')
        }
        wx.reLaunch({
          url: '/pages/index/index'
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    });
  },

  goRegister() {
    wx.navigateTo({
      url: '../register/register'
    })
  },

  goForget() {
    wx.navigateTo({
      url: '../forget/forget',
    })
  },

  changeCheckbox(e) {
    console.log(e);
    this.setData({
      checked: e.detail.value.length ? 1 : 0
    })
  },

  onBlur(e) {
    switch (e.currentTarget.dataset.inputid) {
      case 'phone':
        if (this.data.phoneNumber === '') {
          this.setData({
            ["loginMsg.phone"]: '手机号不可为空'
          })
        } else if (!phoneReg.test(this.data.phoneNumber)) {
          this.setData({
            ["loginMsg.phone"]: '手机号格式不正确'
          })
        } else {
          this.setData({
            ["loginMsg.phone"]: ''
          })
        }
        break;
      case 'password':
        if (this.data.password === '') {
          this.setData({
            ["loginMsg.password"]: '密码不可为空'
          })
        } else {
          this.setData({
            ["loginMsg.password"]: ''
          })
        }
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      translations: app.globalData.translations
    });
    if(wx.getStorageSync('account')) {
      let account = wx.getStorageSync('account')
      this.setData({
        phoneNumber: account.phoneNumber,
        password: account.password,
        checked: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})